﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;
using System.IO;

namespace University.Workshop.WebApp
{
    public partial class ProductCompleteInformation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            productId.Text = Request["ProductID"];
            Name.Text = Request["ProductName"];
            category.Text = Request["ProductCategory"];
            Product prod = ProductDataAccess.GetProductById(Convert.ToInt64(Request["ProductID"]));
            loadProductInfo(prod);
        }


        public void loadProductInfo(Product prod)
        {
            Collection<long> products = ProductDataAccess.GetProductCompleteInfo(prod.ProductId);
            if (products != null && products.Count != 0)
            {
                
                lvHeader.Text = "Other Details";
                lvHeader.Font.Bold = true;
                lvHeader.Font.Size = FontUnit.XLarge;
                lvHeader.ForeColor = System.Drawing.Color.FromName("#400040");
                TableRow tr = new TableRow();
                for (int i = 0; i < 1; i++)
                {
                    TableCell td = new TableCell();
                    ProductCompleteInfo p = (ProductCompleteInfo)Page.LoadControl("~/ProductCompleteInfo.ascx");
                    Product product = ProductDataAccess.GetProductById(products[i]);
                    p.img.ImageUrl = "~/Images/noimage2.jpg";
                    if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                        FileInfo[] currentImages = dir.GetFiles();
                        string thumbUrl = "Thumbnails/" + product.ProductId + "/";
                        if (currentImages.Length > 0)
                        {
                            p.img.ImageUrl = thumbUrl + currentImages[0].Name;
                        }
                    }
                    p.hLink.Text = product.Name;
                    p.link += product.ProductId + "&ProductName=" + product.Name + "&ProductCategory=" + product.Category + "&ProductPrice=" + product.Price;
                    p.lCategory.Text = "Categoria: " + product.Category;
                    p.lPrice.Text = "Precio: " + product.Price;
                    if (product.Currency == "Dollars")
                        p.lPrice.Text += "$us.";
                    else
                    p.lPrice.Text += " Bs.";
                    p.lQuantity.Text = "Cantidad: " + product.Quantity;
                    p.lPosDate.Text = "Disponible desde: " + product.Postdate;
                    p.lDescription.Text = "Descripcion: " + product.Description;
                    p.lStatus.Text = "Status: " + product.Status;
                    td.Controls.Add(p);
                    td.VerticalAlign = VerticalAlign.Top;
                    tr.Cells.Add(td);
                }
                productTable.Rows.Add(tr);
            }
            
        }


        protected void LinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductSelected.aspx?ProductID=" + productId.Text + "&ProductName=" + Name.Text + "&ProductCategory=" + category.Text);

        }
    }
}